package zekezang.org;

import common.utils.activity.ActivityUtils;
import common.utils.database.DatabaseHelper;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class BookMarkAddActivity extends Activity {

	private RelativeLayout menuLayout = null;

	private common.utils.activity.MainMenu mainMenu = null;

	private RelativeLayout book_mark_titlesss = null;

	private EditText book_mark_add_title = null;
	private TextView book_mark_add_texturl = null;
	private Button book_mark_add_confirm_button = null;

	private ImageButton title_back_button = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ActivityUtils.getInstance().setScreenOrientation(this);

		setContentView(R.layout.book_mark_add);

		book_mark_titlesss = (RelativeLayout) findViewById(R.id.book_mark_titlesss);

		menuLayout = (RelativeLayout) findViewById(R.id.menu_layout);
		menuLayout.setVisibility(View.VISIBLE);

		footerSetting();

		book_mark_add_title = (EditText) findViewById(R.id.book_mark_add_title);
		book_mark_add_texturl = (TextView) findViewById(R.id.book_mark_add_texturl);
		book_mark_add_confirm_button = (Button) findViewById(R.id.book_mark_add_confirm_button);

		title_back_button = (ImageButton) findViewById(R.id.title_back_button);

		title_back_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				BookMarkAddActivity.this.finish();
			}
		});

		book_mark_add_confirm_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				bookMarkInSqlite(book_mark_add_texturl.getText().toString(), book_mark_add_title.getText().toString());
				ActivityUtils.getInstance().leftIN_rightOUT_close_Transition(BookMarkAddActivity.this, BookMarkAddActivity.this,
						BookMarkActivity.class);
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		int[] i = ActivityUtils.getInstance().getWidthHight(this);
		RelativeLayout.LayoutParams params = (LayoutParams) book_mark_titlesss.getLayoutParams();
		params.topMargin = (i[1] - 65 - 120) / 5;

		Intent intent = getIntent();
		String[] uri = intent.getStringExtra("uriData").split("###");
		book_mark_add_title.setText(uri[1]);
		book_mark_add_texturl.setText(uri[0]);
	}

	private void footerSetting() {
		mainMenu = common.utils.activity.MainMenu.getInstance(this, this, menuLayout, null, "147", null);
		// menu向上键
		ImageButton menu_up_button = (ImageButton) menuLayout.findViewById(R.id.menu_up_button);
		menu_up_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnKeyDown();
			}
		});
		// menu主页
		final ImageButton menu_home_button = (ImageButton) menuLayout.findViewById(R.id.menu_home_button);
		menu_home_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance().to_home_Transition(BookMarkAddActivity.this, getApplicationContext(), MainActivity.class);
			}
		});
		// menu后退
		final ImageButton menu_back_button = (ImageButton) menuLayout.findViewById(R.id.menu_back_button);
		menu_back_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance().back_leftOUT_rightIN_Transition(BookMarkAddActivity.this, getApplicationContext());
			}
		});
		// menu前进
		final ImageButton menu_go_button = (ImageButton) menuLayout.findViewById(R.id.menu_go_button);
		menu_go_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance().forward_leftOUT_rightIN_Transition(BookMarkAddActivity.this, getApplicationContext());
			}
		});
	}

	private SQLiteDatabase sqLiteDatabase = null;

	public void bookMarkInSqlite(String url, String title) {

		sqLiteDatabase = new DatabaseHelper(this, "book_DB").getReadableDatabase();
		Cursor cursor = sqLiteDatabase.rawQuery("select count(id) as ids from book where uri = ? and uriDescription=?", new String[] { url, title });
		Integer sign = 0;
		while (cursor.moveToNext()) {
			sign = cursor.getInt(cursor.getColumnIndex("ids"));
		}
		sqLiteDatabase.close();
		if (sign <= 0) {
			sqLiteDatabase = new DatabaseHelper(this, "book_DB").getWritableDatabase();
			try {
				sqLiteDatabase.beginTransaction();
				sqLiteDatabase.execSQL("insert into book (uriDescription,uri) values (?, ?)", new Object[] { title, url });
				sqLiteDatabase.setTransactionSuccessful();
				Toast.makeText(this, R.string.save_book_prompt_success, Toast.LENGTH_SHORT).show();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				sqLiteDatabase.endTransaction();
				sqLiteDatabase.close();
			}
		} else {
			Toast.makeText(this, R.string.save_book_prompt, Toast.LENGTH_SHORT).show();
		}
	}

}
